<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>NFA.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="NFA code in Java">
<meta NAME="TITLE" CONTENT="NFA code in Java">
<meta NAME="KEYWORDS" CONTENT="NFA,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>NFA.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "NFA.java">NFA.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac NFA.java</span>
<span class="comment"> *  Execution:    java NFA regexp text</span>
<span class="comment"> *  Dependencies: Stack.java Bag.java Digraph.java DirectedDFS.java</span>
<span class="comment"> *</span>
<span class="comment"> *  % java NFA "(A*B|AC)D" AAAABD</span>
<span class="comment"> *  true</span>
<span class="comment"> *</span>
<span class="comment"> *  % java NFA "(A*B|AC)D" AAAAC</span>
<span class="comment"> *  false</span>
<span class="comment"> *</span>
<span class="comment"> *  % java NFA "(a|(bc)*d)*" abcbcd</span>
<span class="comment"> *  true</span>
<span class="comment"> *</span>
<span class="comment"> *  % java NFA "(a|(bc)*d)*" abcbcbcdaaaabcbcdaaaddd</span>
<span class="comment"> *  true</span>
<span class="comment"> *</span>
<span class="comment"> *  Remarks</span>
<span class="comment"> *  -----------</span>
<span class="comment"> *  The following features are not supported:</span>
<span class="comment"> *    - The + operator</span>
<span class="comment"> *    - Multiway or</span>
<span class="comment"> *    - Metacharacters in the text</span>
<span class="comment"> *    - Character classes.</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="comment">/**</span>
<span class="comment"> *  The </span><span class="keyword">&lt;tt&gt;</span><span class="comment">NFA</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> class provides a data type for creating a</span>
<span class="comment"> *  </span><span class="keyword">&lt;em&gt;</span><span class="comment">nondeterministic finite state automaton</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> (NFA) from a regular</span>
<span class="comment"> *  expression and testing whether a given string is matched by that regular</span>
<span class="comment"> *  expression.</span>
<span class="comment"> *  It supports the following operations: </span><span class="keyword">&lt;em&gt;</span><span class="comment">concatenation</span><span class="keyword">&lt;/em&gt;</span><span class="comment">,</span>
<span class="comment"> *  </span><span class="keyword">&lt;em&gt;</span><span class="comment">closure</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">binary or</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, and </span><span class="keyword">&lt;em&gt;</span><span class="comment">parentheses</span><span class="keyword">&lt;/em&gt;</span><span class="comment">.</span>
<span class="comment"> *  It does not support </span><span class="keyword">&lt;em&gt;</span><span class="comment">mutiway or</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">character classes</span><span class="keyword">&lt;/em&gt;</span><span class="comment">,</span>
<span class="comment"> *  </span><span class="keyword">&lt;em&gt;</span><span class="comment">metacharacters</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> (either in the text or pattern),</span>
<span class="comment"> *  </span><span class="keyword">&lt;em&gt;</span><span class="comment">capturing capabilities</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">greedy</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> or </span><span class="keyword">&lt;em&gt;</span><span class="comment">relucantant</span><span class="keyword">&lt;/em&gt;</span>
<span class="comment"> *  modifiers, and other features in industrial-strength implementations</span>
<span class="comment"> *  such as {</span><span class="type">@link</span><span class="comment"> java.util.regex.Pattern} and {</span><span class="type">@link</span><span class="comment"> java.util.regex.Matcher}.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  This implementation builds the NFA using a digraph and a stack</span>
<span class="comment"> *  and simulates the NFA using digraph search (see the textbook for details).</span>
<span class="comment"> *  The constructor takes time proportional to </span><span class="keyword">&lt;em&gt;</span><span class="comment">M</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, where </span><span class="keyword">&lt;em&gt;</span><span class="comment">M</span><span class="keyword">&lt;/em&gt;</span>
<span class="comment"> *  is the number of characters in the regular expression.</span>
<span class="comment"> *  The </span><span class="keyword">&lt;em&gt;</span><span class="comment">recognizes</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> method takes time proportional to </span><span class="keyword">&lt;em&gt;</span><span class="comment">M N</span><span class="keyword">&lt;/em&gt;</span><span class="comment">,</span>
<span class="comment"> *  where </span><span class="keyword">&lt;em&gt;</span><span class="comment">N</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> is the number of characters in the text.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  For additional documentation,</span>
<span class="comment"> *  see </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://algs4.cs.princeton.edu/54regexp"</span><span class="keyword">&gt;</span><span class="comment">Section 5.4</span><span class="keyword">&lt;/a&gt;</span><span class="comment"> of</span>
<span class="comment"> *  </span><span class="keyword">&lt;i&gt;</span><span class="comment">Algorithms, 4th Edition</span><span class="keyword">&lt;/i&gt;</span><span class="comment"> by Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Robert Sedgewick</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Kevin Wayne</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">NFA</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span>

<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Digraph</span><span class="normal"> G</span><span class="symbol">;</span><span class="normal">         </span><span class="comment">// digraph of epsilon transitions</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> regexp</span><span class="symbol">;</span><span class="normal">     </span><span class="comment">// regular expression</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="normal"> M</span><span class="symbol">;</span><span class="normal">             </span><span class="comment">// number of characters in regular expression</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Initializes the NFA from the specified regular expression.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  regexp the regular expression</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">NFA</span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> regexp</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">regexp </span><span class="symbol">=</span><span class="normal"> regexp</span><span class="symbol">;</span>
<span class="normal">        M </span><span class="symbol">=</span><span class="normal"> regexp</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">();</span>
<span class="normal">        </span><span class="usertype">Stack&lt;Integer&gt;</span><span class="normal"> ops </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Stack</span><span class="symbol">&lt;</span><span class="normal">Integer</span><span class="symbol">&gt;();</span><span class="normal"> </span>
<span class="normal">        G </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Digraph</span><span class="symbol">(</span><span class="normal">M</span><span class="symbol">+</span><span class="number">1</span><span class="symbol">);</span><span class="normal"> </span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> M</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> lp </span><span class="symbol">=</span><span class="normal"> i</span><span class="symbol">;</span><span class="normal"> </span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">regexp</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="string">'('</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> regexp</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="string">'|'</span><span class="symbol">)</span><span class="normal"> </span>
<span class="normal">                ops</span><span class="symbol">.</span><span class="function">push</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">);</span><span class="normal"> </span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">regexp</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="string">')'</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="type">int</span><span class="normal"> or </span><span class="symbol">=</span><span class="normal"> ops</span><span class="symbol">.</span><span class="function">pop</span><span class="symbol">();</span><span class="normal"> </span>

<span class="normal">                </span><span class="comment">// 2-way or operator</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">regexp</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">or</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="string">'|'</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span>
<span class="normal">                    lp </span><span class="symbol">=</span><span class="normal"> ops</span><span class="symbol">.</span><span class="function">pop</span><span class="symbol">();</span>
<span class="normal">                    G</span><span class="symbol">.</span><span class="function">addEdge</span><span class="symbol">(</span><span class="normal">lp</span><span class="symbol">,</span><span class="normal"> or</span><span class="symbol">+</span><span class="number">1</span><span class="symbol">);</span>
<span class="normal">                    G</span><span class="symbol">.</span><span class="function">addEdge</span><span class="symbol">(</span><span class="normal">or</span><span class="symbol">,</span><span class="normal"> i</span><span class="symbol">);</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">                </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">regexp</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">or</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="string">'('</span><span class="symbol">)</span>
<span class="normal">                    lp </span><span class="symbol">=</span><span class="normal"> or</span><span class="symbol">;</span>
<span class="normal">                </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">assert</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span><span class="normal"> </span>

<span class="normal">            </span><span class="comment">// closure operator (uses 1-character lookahead)</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">i </span><span class="symbol">&lt;</span><span class="normal"> M</span><span class="symbol">-</span><span class="number">1</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> regexp</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">+</span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="string">'*'</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span>
<span class="normal">                G</span><span class="symbol">.</span><span class="function">addEdge</span><span class="symbol">(</span><span class="normal">lp</span><span class="symbol">,</span><span class="normal"> i</span><span class="symbol">+</span><span class="number">1</span><span class="symbol">);</span><span class="normal"> </span>
<span class="normal">                G</span><span class="symbol">.</span><span class="function">addEdge</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">+</span><span class="number">1</span><span class="symbol">,</span><span class="normal"> lp</span><span class="symbol">);</span><span class="normal"> </span>
<span class="normal">            </span><span class="cbracket">}</span><span class="normal"> </span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">regexp</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="string">'('</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> regexp</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="string">'*'</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> regexp</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="string">')'</span><span class="symbol">)</span><span class="normal"> </span>
<span class="normal">                G</span><span class="symbol">.</span><span class="function">addEdge</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">,</span><span class="normal"> i</span><span class="symbol">+</span><span class="number">1</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ops</span><span class="symbol">.</span><span class="function">size</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span>
<span class="normal">            </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Invalid regular expression"</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span><span class="normal"> </span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns true if the text is matched by the regular expression.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  txt the text</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if the text is matched by the regular expression,</span>
<span class="comment">     *         </span><span class="keyword">&lt;tt&gt;</span><span class="comment">false</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> otherwise</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">recognizes</span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> txt</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">DirectedDFS</span><span class="normal"> dfs </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">DirectedDFS</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">Bag&lt;Integer&gt;</span><span class="normal"> pc </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Bag</span><span class="symbol">&lt;</span><span class="normal">Integer</span><span class="symbol">&gt;();</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> v</span><span class="symbol">++)</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">dfs</span><span class="symbol">.</span><span class="function">marked</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">))</span><span class="normal"> pc</span><span class="symbol">.</span><span class="function">add</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// Compute possible NFA states for txt[i+1]</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> txt</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">();</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">txt</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="string">'*'</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> txt</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="string">'|'</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> txt</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="string">'('</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> txt</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="string">')'</span><span class="symbol">)</span>
<span class="normal">                </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"text contains the metacharacter '"</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> txt</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="string">"'"</span><span class="symbol">);</span>

<span class="normal">            </span><span class="usertype">Bag&lt;Integer&gt;</span><span class="normal"> match </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Bag</span><span class="symbol">&lt;</span><span class="normal">Integer</span><span class="symbol">&gt;();</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">:</span><span class="normal"> pc</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">v </span><span class="symbol">==</span><span class="normal"> M</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">continue</span><span class="symbol">;</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">((</span><span class="normal">regexp</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> txt</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">))</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> regexp</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="string">'.'</span><span class="symbol">)</span>
<span class="normal">                    match</span><span class="symbol">.</span><span class="function">add</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">+</span><span class="number">1</span><span class="symbol">);</span><span class="normal"> </span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            dfs </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">DirectedDFS</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">,</span><span class="normal"> match</span><span class="symbol">);</span><span class="normal"> </span>
<span class="normal">            pc </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Bag</span><span class="symbol">&lt;</span><span class="normal">Integer</span><span class="symbol">&gt;();</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> v</span><span class="symbol">++)</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">dfs</span><span class="symbol">.</span><span class="function">marked</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">))</span><span class="normal"> pc</span><span class="symbol">.</span><span class="function">add</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">);</span>

<span class="normal">            </span><span class="comment">// optimization if no states reachable</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">pc</span><span class="symbol">.</span><span class="function">size</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// check for accept state</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">:</span><span class="normal"> pc</span><span class="symbol">)</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">v </span><span class="symbol">==</span><span class="normal"> M</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Unit tests the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">NFA</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> data type.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">String</span><span class="normal"> regexp </span><span class="symbol">=</span><span class="normal"> </span><span class="string">"("</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> args</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="string">")"</span><span class="symbol">;</span>
<span class="normal">        </span><span class="usertype">String</span><span class="normal"> txt </span><span class="symbol">=</span><span class="normal"> args</span><span class="symbol">[</span><span class="number">1</span><span class="symbol">];</span>
<span class="normal">        </span><span class="usertype">NFA</span><span class="normal"> nfa </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NFA</span><span class="symbol">(</span><span class="normal">regexp</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">nfa</span><span class="symbol">.</span><span class="function">recognizes</span><span class="symbol">(</span><span class="normal">txt</span><span class="symbol">));</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="cbracket">}</span><span class="normal"> </span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
